package work.database.example.connector;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {
	private String driver;
	private String url;
	private String id;
	private String password;
	
	private static ConnectionFactory instance = new ConnectionFactory();
	
	public static Connection getConnection(){
		return instance.creatConnection();
	}

	private Connection creatConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url,id,password);
			System.out.println("MySQL Connection success");
		} catch (SQLException e) {		
			System.out.println("Error:Unable connect to mysql");
		}
		return conn;
	}

	private ConnectionFactory() {
		loaProperties();
			try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				System.err.println("Driver not found");
				System.exit(-1);
			}			
	}

	private void loaProperties() {
		Properties prop = PropertiesDb.loadProperties("dbcon.xml");
		driver = prop.getProperty("DRIVER");
		url = prop.getProperty("URL");
		id = prop.getProperty("ID");
		password = prop.getProperty("PASSWORD");		
	}
}
